package org.kolonitsky.css.runner.cli;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.converters.FileConverter;
import org.kolonitsky.css.runner.Settings;

import java.io.File;
import java.net.URISyntaxException;

import static org.apache.commons.io.FileUtils.getFile;

/**
 * @author Alex.Kolonitsky
 */
public class CliSettings implements Settings {

    @Parameter(names = "-in", description = "cssInputFolder", converter = FileConverter.class)
    private File cssInputFolder;

    @Parameter(names = "-xml", description = "xmlOutputFolder", converter = FileConverter.class)
    private File xmlOutputFolder;

    @Parameter(names = "-template", description = "htmlTemplateFolder", converter = FileConverter.class)
    private File htmlTemplateFolder;

    @Parameter(names = "-out", description = "htmlOutputFolder", converter = FileConverter.class)
    private File htmlOutputFolder;

    @Parameter(names = "-xslt", description = "folder with xslt transformations", converter = FileConverter.class)
    private File xslt;

    public File cssInputFolder() {
        if (cssInputFolder == null) {
            cssInputFolder = resource(".");
        }
        return cssInputFolder;
    }

    public File htmlOutputFolder() {
        if (htmlOutputFolder == null) {
            htmlOutputFolder = cssInputFolder();
        }
        return htmlOutputFolder;
    }

    public File xmlOutputFolder() {
        if (xmlOutputFolder == null) {
            xmlOutputFolder = getFile(htmlOutputFolder(), "xml");
        }
        return xmlOutputFolder;
    }

    public File htmlTemplateFolder() {
        if (htmlTemplateFolder == null) {
            htmlTemplateFolder = resource("template/default");
        }
        return htmlTemplateFolder;
    }

    public File xslt() {
        if (xslt == null) {
            xslt = getFile(htmlTemplateFolder(), "xslt");
        }
        return xslt;
    }

    private File resource(String name) {
        try {
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            return new File(classLoader.getResource(name).toURI());
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return null;
    }
}
